'use strict';
const Service = require('egg').Service;
class CategoryService extends Service {
  async addCategory(data) {
    const res = await this.app.mysql.insert('category', data);
    if (res.affectedRows === 1) {
      return {
        code: 0,
      };
    }
    return {
      code: -1,
    };
  }
  // 获取商品分类
  async getCategoryList() {
    const res = await this.app.mysql.select('category', {
      where: { parent_id: 0 },
      orders: [[ 'sort_order', 'asc' ]],
    });
    return res;
  }

  // 根据id获取商品分类
  async getCategoryById(id) {
    const res = await this.app.mysql.get('category', { id });
    return res;
  }

  // 根据id修改分类
  async updateCategory(data) {
    const res = await this.app.mysql.update('category', data);
    if (res.affectedRows === 1) {
      return {
        code: 0,
      };
    }
    return {
      code: -1,
    };

  }
  // 删除分类
  async deleteCategory(data) {
    const res = await this.app.mysql.delete('category', data);
    if (res.affectedRows === 1) {
      return {
        code: 0,
      };
    }
    return {
      code: -1,
    };
  }
}
module.exports = CategoryService;
